﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;
using Xpedite.Config;
using Xpedite.Extensions;
using Xpedite.Mvc.Extensions;
using Xpedite.Resources;
using Xpedite.Resources.Definitions;
using WebContext = Xpedite.Mvc.WebAbstraction.WebContext;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Mvc.Html
{
  public static class CompositeResourceExtensions
  {
    private static readonly ConfigMode Mode = ConfigSectionHandler.Instance.Mode;

    public static MvcHtmlString CompositeResource(this HtmlHelper htmlHelper, ResourceType resourceType, params String[] resources)
    {
      Verify.NotNull(htmlHelper, "htmlHelper");
      Verify.NotNull(resources, "resources");

      return htmlHelper.CompositeResource(resourceType, ResourceNamingUtility.GetDefaultName(resources), resources);
    }

    public static MvcHtmlString CompositeResource(this HtmlHelper htmlHelper, ResourceType resourceType, IEnumerable<String> resources)
    {
      Verify.NotNull(htmlHelper, "htmlHelper");
      Verify.NotNull(resources, "resources");

      return htmlHelper.CompositeResource(resourceType, ResourceNamingUtility.GetDefaultName(resources), resources);
    }

    public static MvcHtmlString CompositeResource(this HtmlHelper htmlHelper, ResourceType resourceType, params Resource[] resources)
    {
      Verify.NotNull(htmlHelper, "htmlHelper");
      Verify.NotNull(resources, "resources");

      return htmlHelper.CompositeResource(resourceType, ResourceNamingUtility.GetDefaultName(resources), resources);
    }

    public static MvcHtmlString CompositeResource(this HtmlHelper htmlHelper, ResourceType resourceType, IEnumerable<Resource> resources)
    {
      Verify.NotNull(htmlHelper, "htmlHelper");
      Verify.NotNull(resources, "resources");

      return htmlHelper.CompositeResource(resourceType, ResourceNamingUtility.GetDefaultName(resources), resources);
    }

    public static MvcHtmlString CompositeResource(this HtmlHelper htmlHelper, ResourceType resourceType, String referenceName, IEnumerable<String> resources)
    {
      Verify.NotNull(htmlHelper, "htmlHelper");
      Verify.NotNull(resources, "resources");
      Verify.NotWhiteSpace(referenceName, "referenceName");
      
      var webContext = new WebContext(htmlHelper.ViewContext.HttpContext);

      return htmlHelper.CompositeResource(
        webContext.GetResourceDefinitionFactory().Create(resourceType, referenceName),
        resources.Where(resource => !String.IsNullOrEmpty(resource))
                 .Select(resource => NamedValue.Create(ResourceNamingUtility.GetDefaultName(resource), (IResourceUrl)new ResourceUrl(webContext.Request, resource))),
        Mode == ConfigMode.Off || Mode == ConfigMode.RemoteOnly && webContext.Request.IsLocal
      );
    }

    public static MvcHtmlString CompositeResource(this HtmlHelper htmlHelper, ResourceType resourceType, String referenceName, IEnumerable<Resource> resources)
    {
      Verify.NotNull(htmlHelper, "htmlHelper");
      Verify.NotNull(resources, "resources");
      Verify.NotWhiteSpace(referenceName, "referenceName");

      var webContext = new WebContext(htmlHelper.ViewContext.HttpContext);

      return htmlHelper.CompositeResource(
        webContext.GetResourceDefinitionFactory().Create(resourceType, referenceName),
        resources.Where(resource => resource != null)
                 .Select(resource => NamedValue.Create(resource.Name, (IResourceUrl)new ResourceUrl(webContext.Request, resource.ResourceUrl))),
        Mode == ConfigMode.Off || Mode == ConfigMode.RemoteOnly && webContext.Request.IsLocal
      );
    }

    private static MvcHtmlString CompositeResource(this HtmlHelper htmlHelper, IResourceDefinition definition, IEnumerable<INamedValue<IResourceUrl>> resources, Boolean debugMode)
    {
      Verify.NotNull(htmlHelper, "htmlHelper");
      Verify.NotNull(definition, "definition");
      Verify.NotNull(resources, "resources");

      definition.ReferenceResources(resources);

      htmlHelper.StoreDefinition(definition);

      using (var stringWriter = new StringWriter())
      using (var htmlWriter = new HtmlTextWriter(stringWriter))
      {
        if (debugMode)
          htmlWriter.RenderDebuggableDefinition(definition);
        else
          htmlWriter.RenderCompositeDefinition(definition);

        return MvcHtmlString.Create(stringWriter.ToString());
      }
    }
  }
}
